/*
    红点提示判断逻辑
*/

import WxData from './wxData';

import { GetDate } from './utils';

const GetDateUtil = new GetDate()

class ExhibitDates {
    /*
        展览出现的日期
    */

    constructor(){
        this.subscribe = new Set()
        this.suggest = new Set()
    }

    update(config){

        /*
            现有数据
        */

        if(config && this.isValidSubscribe(config.subscribe)){
            config.subscribe.forEach( item =>{
                this.subscribe.add(item)
            })
        }

        if(config && this.isValidSuggest(config.suggest)){
            config.suggest.forEach( item =>{
                this.suggest.add(item)
            })
        }
    }

    clear(){
        this.subscribe.clear()
        this.suggest.clear()
    }

    isValidSuggest(suggest){
        return Array.isArray(suggest) && suggest.length > 0
    }

    isValidSubscribe(subscribe){
        return Array.isArray(subscribe) && subscribe.length > 0
    }

    hasSubscribeExhibition(day){
        return typeof day == "string" && this.subscribe.has(day)
    }

    hasSuggestExhibition(day){
        return typeof day == "string" && this.suggest.has(day)
    }
}

const exhibitModel = new ExhibitDates()

class Badage extends WxData{

    constructor(component) {
        super(component);
        this.Component = component;
    }

    getCalendarConfig() {
        return this.Component.config;
    }

    refreshSpots(){
        const calendar = this.getData('calendar');
        const dates = [...calendar.days];
        dates.forEach( item => {
            let date = GetDateUtil.newDate(item.year,item.month,item.day).format("yyyy-MM-dd")
            item.exhibit = {
                "subscribe":exhibitModel.hasSubscribeExhibition(date),
                "suggest":exhibitModel.hasSuggestExhibition(date)
            }
        })
        const o = {
            'calendar.days': dates,
        };
        this.setData(o);
    }

    setExhibitionBadage(config){
        /*
            设置展览出现的时间数据
        */
        const CalendarConfig = this.getCalendarConfig();
        if(CalendarConfig.showExhibitionBadage){
            if(exhibitModel.isValidSubscribe(config.subscribe) || exhibitModel.isValidSuggest(config.suggest)){
                exhibitModel.update(config)
                this.refreshSpots()
            }
        }
    }

    clearBadage(){
        exhibitModel.clear()
    }
}

export default component => new Badage(component);
